export function renderMixin(Vue) {
  // 标签
  Vue.prototype._c = function () {
    return createElement(this, ...arguments);
  }
  // 文本
  Vue.prototype._v = function (text) {
    return createText(text)
  }
  // 变量
  Vue.prototype._s = function (val) {
    return val === null ? "" : (typeof val === 'object') ? JSON.stringify(val) : val;
  }


  Vue.prototype._render = function () {
    let vm = this;
    let render = vm.$options.render;
    let vnode = render.call(this);
    return vnode
  }
}

/**
 * 创建元素
 * @param {*} tag 
 * @param {*} data 
 * @param  {...any} children 
 * @returns 
 */
function createElement(vm, tag, data = {}, ...children) {
  if (isResvr(tag)) {
    // 标签处理
    return vnode(vm, tag, data, data.key, children);
  } else {
    // 组件处理
    const Ctor = vm.$options['components'][tag]; // 获取到当前的组件
    return createComponent(vm, tag, data, children, Ctor);
  }
}
// 是否是原生标签
function isResvr(tag) {
  return ['a', 'div', 'h', 'button', 'span', 'input'].includes(tag);
}
/**
 * 创建组件虚拟dom
 */
function createComponent(vm, tag, data, children, Ctor) {
  if (typeof Ctor === 'object') {
    Ctor = vm.constructor.extend(Ctor);
  }
  data.hook = {
    // 初始化子组件，创建实例
    init(vnode) {
      let child = vnode.componentInstance = new vnode.componentOptions.Ctor({});
      child.$mount();
    }
  }
  return vnode(vm, 'vue-component' + tag, data, undefined, undefined, undefined, { Ctor, children });
}
/**
 * 创建文本
 * @param {*} text 
 * @returns 
 */
function createText(text) {
  return vnode(undefined, undefined, undefined, undefined, text)
}
/**
 * 虚拟DOM
 * @param {*} tag 
 * @param {*} data 
 * @param {*} key 
 * @param {*} children 
 * @param {*} text 
 * @param {*} componentOptions 组件属性 
 * @returns 
 */
function vnode(vm, tag, data, key, children, text, componentOptions) {
  return {
    vm, tag, data, key, children, text, componentOptions
  }
}